package com.ximalaya.demo;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.lang.ref.WeakReference;

/**
 * Time: 2022/3/29 11:07 上午
 * Author: leixun
 * Email: leixun33@163.com
 * <p>
 * Description:
 */
public class LiveCourseChooseAdapter {

    private static  final String TAG = LiveCourseChooseAdapter.class.getSimpleName();
    private volatile int currentPage = 0;
    private volatile boolean r = false;
    private volatile boolean s = false;
    private float t = 1.21F;
    private float u = 1.21F;
    private Handler v = null;

    private int pageSize = 1;

    // 处理压住的底部两张图片
    private void b(final LiveItemHolder parama, final int paramInt)
    {
        AnimatorSet localAnimatorSet = new AnimatorSet();
        final FrameLayout nextCardFl = parama.u[((this.currentPage + 1) % paramInt)];
        FrameLayout nextCardMask = parama.v[((this.currentPage + 1) % paramInt)];
        final FrameLayout nextCardPlayFl = parama.x[((this.currentPage + 1) % paramInt)];

        localAnimatorSet.playTogether(new Animator[] {
                ObjectAnimator.ofFloat(nextCardFl, "scaleX", new float[] { 1.0F, this.u }),
                ObjectAnimator.ofFloat(nextCardFl, "scaleY", new float[] { 1.0F, this.u }),
                ObjectAnimator.ofFloat(nextCardFl, "rotation", new float[] { -10.0F, 0.0F }),
                ObjectAnimator.ofFloat(nextCardFl, "translationY", new float[] { 0.0F, 54.0F }),
                ObjectAnimator.ofFloat(nextCardFl, "translationX", new float[] { 0.0F, 8.0F }),
                ObjectAnimator.ofFloat(nextCardMask, "alpha", new float[] { 0.6F, 0.0F }) });

        // 如果是3个的话 还得处理 next next
        if (paramInt == 3)
        {
            FrameLayout nextNextCardFl = parama.u[((this.currentPage + 2) % paramInt)];
            localAnimatorSet.playTogether(
                    new Animator[] {
                            ObjectAnimator.ofFloat(nextNextCardFl, "scaleX", new float[] { 1.0F, this.t }),
                            ObjectAnimator.ofFloat(nextNextCardFl, "scaleY", new float[] { 1.0F, this.t }),
                            ObjectAnimator.ofFloat(nextNextCardFl, "rotation", new float[] { -25.0F, -10.0F }),
                            ObjectAnimator.ofFloat(nextNextCardFl, "translationY", new float[] { 0.0F, 54.0F }),
                            ObjectAnimator.ofFloat(nextNextCardFl, "translationX", new float[] { 0.0F, 14.0F }) });
        }

        localAnimatorSet.setDuration(250L);
        localAnimatorSet.addListener(new Animator.AnimatorListener()
        {
            public void onAnimationCancel(Animator paramAnonymousAnimator) {}

            public void onAnimationEnd(Animator paramAnonymousAnimator)
            {
                nextCardFl.setScaleX(1.0F);
                nextCardFl.setScaleY(1.0F);
                nextCardFl.setTranslationX(0.0F);
                nextCardFl.setTranslationY(0.0F);
                nextCardFl.setLayoutParams(parama.z[0]);
                nextCardPlayFl.setVisibility(View.VISIBLE);
                if (paramInt == 3)
                {
                    FrameLayout nextNextCardFl = parama.u[(currentPage + 2) % paramInt];
                    nextNextCardFl.setScaleX(1.0F);
                    nextNextCardFl.setScaleY(1.0F);
                    nextNextCardFl.setTranslationX(0.0F);
                    nextNextCardFl.setTranslationY(0.0F);
                    nextNextCardFl.setLayoutParams(parama.z[1]);
                }
                currentPage = currentPage+1;
                localAnimatorSet.cancel();
            }

            public void onAnimationRepeat(Animator paramAnonymousAnimator) {}

            public void onAnimationStart(Animator paramAnonymousAnimator) {}
        });
        localAnimatorSet.start();
    }

    // 最上层 card 先消失 然后进行垫底操作
    void a(final LiveItemHolder parama, final int paramInt)
    {
        if ((!this.r) && (paramInt > 1) && (paramInt <= 3)
        )
        {
            Log.d(TAG,"start");
//            this.r = true;
            final FrameLayout topCardFl = parama.u[(this.currentPage % paramInt)];
            final FrameLayout topCardPlayFl = parama.x[(this.currentPage % paramInt)];

            // card frame 处理
            ObjectAnimator localObjectAnimator1 =
                    ObjectAnimator.ofFloat(topCardFl, "rotation", new float[] { 0.0F, 10.0F });
            ObjectAnimator localObjectAnimator2 =
                    ObjectAnimator.ofFloat(topCardFl, "alpha", new float[] { 1.0F, 0.0F });
            ObjectAnimator localObjectAnimator3 =
                    ObjectAnimator.ofFloat(topCardFl, "translationY", new float[] { 0.0F, 74.0F });

            final FrameLayout topCardMask = parama.v[(this.currentPage % paramInt)];
            // mask消失
            topCardMask.setAlpha(0.0F);

            AnimatorSet localAnimatorSet = new AnimatorSet();
            localAnimatorSet.playTogether(new Animator[] { localObjectAnimator1, localObjectAnimator2, localObjectAnimator3 });
            localAnimatorSet.setDuration(150L);

            localAnimatorSet.addListener(new Animator.AnimatorListener()
            {
                public void onAnimationCancel(Animator paramAnonymousAnimator) {}

                public void onAnimationEnd(Animator paramAnonymousAnimator)
                {
                    if ((topCardFl.getParent() instanceof ViewGroup)) {
                        ((ViewGroup)topCardFl.getParent()).removeView(topCardFl);
                    }

                    topCardFl.setTranslationY(0.0F);
                    topCardFl.setAlpha(1.0F);
                    topCardMask.setAlpha(0.6F);
                    topCardPlayFl.setVisibility(View.INVISIBLE);

                    int i = pageSize;
                    if (i == 3)
                    {
                        topCardFl.setRotation(-25.0F);
                        parama.c.addView(topCardFl, 1, parama.z[2]);
                    }
                    else if (i == 2)
                    {
                        topCardFl.setRotation(-10.0F);
                        parama.c.addView(topCardFl, 2, parama.z[1]);
                    }
                    b(parama,pageSize);
                    localAnimatorSet.cancel();
                }

                public void onAnimationRepeat(Animator paramAnonymousAnimator) {}

                public void onAnimationStart(Animator paramAnonymousAnimator) {}
            });
            localAnimatorSet.start();
            return;
        }
    }

    public void start(LiveItemHolder itemHolder,Context context){
        currentPage = 0;
        pageSize =3;
//        removeMsg();

//        this.v = new MyHandler(itemHolder);
//        sendMsg();
    }

    private void sendMsg()
    {
        Object localObject = this.v;
        if (localObject != null)
        {
            localObject = ((Handler)localObject).obtainMessage(1);
            this.v.sendMessageDelayed((Message)localObject, 2000L);
        }
    }

    private void removeMsg()
    {
        Handler localHandler = this.v;
        if ((localHandler != null) && (localHandler.hasMessages(1))) {
            this.v.removeMessages(1);
        }
    }

    static class LiveItemHolder{

        View a;
        TextView b;
        RelativeLayout c;
        ImageView d;  // card1
        ImageView e;// card2
        ImageView f;    // card 3
        ImageView g;    // card_1_play_iv
        ImageView h;// card_2_play_iv
        ImageView i;// card_3_play_iv
        FrameLayout j; //card_1_play_fl
        FrameLayout k;//card_2_play_fl
        FrameLayout l;//card_3_play_fl
        FrameLayout m;// card_1_fl
        FrameLayout n;//card_2_fl
        FrameLayout o;//card_3_fl
        FrameLayout p;// card_1_mask
        FrameLayout q;//card_2_mask
        FrameLayout r;//card_3_mask
        BannerIndicator s;  // main_banner_indicator
        ControlViewpager t; // ControlViewpager
        FrameLayout[] u;    // card_1_fl card_2_fl card_3_fl
        FrameLayout[] v;    // card_1_mask card_2_mask card_3_mask
        ImageView[] w;  // card_1_play_iv card_2_play_iv card_3_play_iv
        FrameLayout[] x;    // card_1_play_fl card_2_play_fl card_3_play_fl
        ImageView[] y;  // card1 card2 card3
        RelativeLayout.LayoutParams[] z;

        public LiveItemHolder(View paramView)
        {
            this.a = paramView.findViewById(R.id.main_daily_root_rl);
            this.c = ((RelativeLayout)paramView.findViewById(R.id.main_cover_container_fl));
            this.b = ((TextView)paramView.findViewById(R.id.main_daily_choose_title));
            this.f = ((ImageView)paramView.findViewById(R.id.card_3));
            this.e = ((ImageView)paramView.findViewById(R.id.card_2));
            this.d = ((ImageView)paramView.findViewById(R.id.card_1));
            this.g = ((ImageView)paramView.findViewById(R.id.card_1_play_iv));
            this.h = ((ImageView)paramView.findViewById(R.id.card_2_play_iv));
            this.i = ((ImageView)paramView.findViewById(R.id.card_3_play_iv));
            this.j = ((FrameLayout)paramView.findViewById(R.id.card_1_play_fl));
            this.k = ((FrameLayout)paramView.findViewById(R.id.card_2_play_fl));
            this.l = ((FrameLayout)paramView.findViewById(R.id.card_3_play_fl));
            this.m = ((FrameLayout)paramView.findViewById(R.id.card_1_fl));
            this.n = ((FrameLayout)paramView.findViewById(R.id.card_2_fl));
            this.o = ((FrameLayout)paramView.findViewById(R.id.card_3_fl));
            this.p = ((FrameLayout)paramView.findViewById(R.id.card_1_mask));
            this.q = ((FrameLayout)paramView.findViewById(R.id.card_2_mask));
            this.r = ((FrameLayout)paramView.findViewById(R.id.card_3_mask));
            this.s = ((BannerIndicator)paramView.findViewById(R.id.main_banner_indicator));
            this.t = ((ControlViewpager)paramView.findViewById(R.id.main_view_pager));
            paramView = this.m;
            this.u = new FrameLayout[] { this.m, this.n, this.o };
            this.v = new FrameLayout[] { this.p, this.q, this.r };
            this.y = new ImageView[] { this.d, this.e, this.f };
            this.w = new ImageView[] { this.g, this.h, this.i };
            this.x = new FrameLayout[] { this.j, this.k, this.l };
            RelativeLayout.LayoutParams[] arrayOfLayoutParams = new RelativeLayout.LayoutParams[3];
            this.z = arrayOfLayoutParams;
            if(paramView.getLayoutParams() instanceof RelativeLayout.LayoutParams){
                arrayOfLayoutParams[0] = (RelativeLayout.LayoutParams)paramView.getLayoutParams();
            }else{
                arrayOfLayoutParams[0] =null;
            }
            if(this.n.getLayoutParams() instanceof RelativeLayout.LayoutParams){
                arrayOfLayoutParams[1] = (RelativeLayout.LayoutParams)this.n.getLayoutParams();
            }else{
                arrayOfLayoutParams[1] =null;
            }

            if(this.o.getLayoutParams() instanceof RelativeLayout.LayoutParams){
                arrayOfLayoutParams[2] = (RelativeLayout.LayoutParams)this.o.getLayoutParams();
            }else{
                arrayOfLayoutParams[2] =null;
            }

        }

    }

    class MyHandler extends Handler
    {
        private WeakReference<LiveCourseChooseAdapter.LiveItemHolder> b;

        public MyHandler(LiveCourseChooseAdapter.LiveItemHolder parama)
        {
            super();
            this.b = new WeakReference(parama);
        }

        public void handleMessage(Message message)
        {
            if (message.what == 1)
            {
                LiveCourseChooseAdapter.LiveItemHolder itemHolder = null;
                WeakReference localWeakReference = this.b;
                if (localWeakReference != null) {
                    itemHolder = (LiveCourseChooseAdapter.LiveItemHolder)localWeakReference.get();
                }
//                if (com.koolearn.toefl2019.home.adapter.DailyChooseAdapter.a(com.koolearn.toefl2019.home.adapter.DailyChooseAdapter.this))
//                {
//                    return;
//                }
//                if ((com.koolearn.toefl2019.home.adapter.DailyChooseAdapter.this.g()) && (paramMessage != null) && (paramMessage.t != null)) {
//                    paramMessage.t.setCurrentItem(com.koolearn.toefl2019.home.adapter.DailyChooseAdapter.b(com.koolearn.toefl2019.home.adapter.DailyChooseAdapter.this) + 1);
//                }
                if(itemHolder!=null && itemHolder.t!=null){
                    itemHolder.t.setCurrentItem(getNextIndex());
                    currentPage = (currentPage+1)%pageSize;
                    Log.d(TAG,(itemHolder==null)+" "+currentPage);
                }else{
                    Log.d(TAG,(itemHolder==null)+"  itemHOlder==null");
                }
                sendMessageDelayed(obtainMessage(1), 5000L);
            }
            Log.d(TAG,"handleMessage ");

        }
    }

    public int getCurrentItem(){
        return currentPage;
    }

    public int getNextIndex(){
        int index = (currentPage+1) % pageSize;
        Log.d(TAG,"currentPage:"+currentPage+" index:"+index+"");
        currentPage = index;
        return index;
    }
    public void setCurrentIndex(){
//        currentPage = (currentPage+1)%pageSize;
        Log.d(TAG,"currentPage:"+currentPage);
    }
}
